package com.chenfan.mcn.model.vo;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.utils.CommonUtil;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 红人刊例-明细查询VO
 *
 * @author wulg
 * @date 2021-12-30
 **/
@Data
public class StarRateCardDetailQueryVO {

    /**
     * Table头数据
     */
    private List<TableHead> headData;


    /**
     * Table体数据
     */
    private Page<JSONObject> bodyData;

    public StarRateCardDetailQueryVO() {
        this.headData = new ArrayList<>();
        this.headData.add(new TableHead(NumberCst.ZERO, "confirm_status", "确认状态", null, false, false, false, 101, null,"未确认_0", "已确认_1"));
        //this.headData.add(new TableHead(NumberCst.ZERO, "create_date", "创建时间", false, 101));
    }

    public void addTableHead(TableHead... tableHeads) {
        if(ArrayUtils.isNotEmpty(tableHeads)) {
            for(TableHead tableHead : tableHeads) {
                this.headData.add(tableHead);
            }
        }
    }

    public List<TableHead> getHeadData() {
        if(CollectionUtils.isNotEmpty(headData)) {
            return headData.stream().sorted(Comparator.comparing(TableHead::getOrder)).collect(Collectors.toList());
        }
        return headData;
    }

    public void addTableHeads(List<TableHead> tableHeadList) {
        if(CollectionUtils.isNotEmpty(tableHeadList)) {
            this.headData.addAll(tableHeadList);
        }
    }

    @JSONField(serialize = false)
    public List<ExcelExportEntity> getEntityList() {
        if(CollectionUtils.isEmpty(headData)) {
            return new ArrayList<>(0);
        }
        List<TableHead> sortedHeadData =  this.getHeadData();
        List<ExcelExportEntity> entityList = new ArrayList<>(sortedHeadData.size());
        for(TableHead tableHead : sortedHeadData) {
            ExcelExportEntity excelExportEntity = new ExcelExportEntity(tableHead.getTitle(), tableHead.getKey());
            excelExportEntity.setFormat(tableHead.getFormat());
            excelExportEntity.setReplace(tableHead.getReplace());
            excelExportEntity.setOrderNum(tableHead.getOrder());
            if(tableHead.isImage.booleanValue()) {
                excelExportEntity.setExportImageType(2);
                excelExportEntity.setType(2);
                excelExportEntity.setWidth(15);
                excelExportEntity.setHeight(30);
            }
            entityList.add(excelExportEntity);
        }
        return entityList;
    }

    @JSONField(serialize = false)
    public List<Map<String, Object>> getDataSet(String webImagePrefix, Map<String, byte[]> iamgeMap) {
        if(bodyData == null || CollectionUtils.isEmpty(bodyData.getRecords())) {
            return new ArrayList<>(0);
        }
        List<TableHead> sortedHeadData =  this.getHeadData();
        Map<String, TableHead> tableHeadMap = sortedHeadData.stream().collect(Collectors.toMap(k -> k.getKey(), Function.identity()));
        List<String> keyList = sortedHeadData.stream().map(TableHead::getKey).distinct().collect(Collectors.toList());
        List<Map<String, Object>> dataSet = new ArrayList<>(bodyData.getRecords().size());
        for(JSONObject jsonObject : bodyData.getRecords()) {
            Map<String, Object> data = new LinkedHashMap<>(keyList.size());
            for(String key : keyList) {
                Object value = jsonObject.get(key);
                if(Objects.nonNull(value) && tableHeadMap.containsKey(key)) {
                    TableHead tableHead = tableHeadMap.get(key);
                    if(tableHead.isImage.booleanValue()) {
                        String fileId = value.toString();
                        if(!iamgeMap.containsKey(fileId)) {
                            byte[] imageData = CommonUtil.getImageData(webImagePrefix + fileId + "/400X400");
                            iamgeMap.put(fileId, imageData);
                        }
                        data.put(key, iamgeMap.get(fileId));
                        continue;
                    }
                }
                data.put(key, value);
            }
            dataSet.add(data);
        }
        return dataSet;
    }

    /**
     * 前端表头对象
     *
     * @author wulg
     * @date 2022-01-07
     **/
    @Data
    public static class TableHead implements Comparator<Integer> {

        /**
         * 类型：0-公共信息、1-基础信息、2-平台信息、3-自定义信息、4-报价信息
         */
        private Integer type;

        /**
         * 列名
         */
        private String key;

        /**
         * 列标题
         */
        private String title;

        /**
         * 左右对齐方式
         */
        private String align = "left";

        /**
         * 列宽度
         */
        private String width = "90";

        /**
         * 是否默认勾选-编辑页展示
         */
        private boolean autoSelect;

        /**
         * 排序
         */
        private Integer order;

        /**
         * 数据类型（只适用于报价方式）
         */
        private Integer dataType;

        /**
         * 是否必填 默认否
         */
        private Boolean required;

        /** 只适用于Excel导出 */
        @JSONField(serialize = false)
        private String format;

        @JSONField(serialize = false)
        private String[] replace;

        /** 是否是图片 */
        private Boolean isImage;

        public TableHead(Integer type, String key, String title, boolean autoSelect, boolean isImage, Integer order) {
            this(type, key, title, null, false, autoSelect, isImage, order);
        }

        public TableHead(Integer type, String key, String title, Integer dataType, Boolean required, boolean autoSelect, boolean isImage, Integer order) {
            this(type, key, title, dataType, required, autoSelect, isImage, order, null, null);
        }

        public TableHead(Integer type, String key, String title, Integer dataType, Boolean required, boolean autoSelect, boolean isImage, Integer order, String format,  String... replace) {
            this.type = type;
            this.key = key;
            this.title = title;
            this.dataType = dataType;
            this.required = required;
            this.autoSelect = autoSelect;
            this.isImage = isImage;
            this.order = ObjectUtils.defaultIfNull(order, 1);
            this.format = format;
            this.replace = replace;
        }

        @Override
        public int compare(Integer o1, Integer o2) {
            return o1.compareTo(o2);
        }
    }
}
